package com.phenix.mvp.ui.login;

import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.inputmethod.EditorInfo;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.Toast;

import com.phenix.mvp.R;
import com.phenix.mvp.base.mvp.BaseActivity;

import butterknife.BindView;
import lib.phenix.com.views.SwipeBackLayout;

/**
 * @author zhouphenix
 */
public class LoginActivity extends BaseActivity<LoginPresenter> implements LoginPresenter.View {

    @BindView(R.id.toolbar)
    Toolbar mToolbar;

    /**UI references.*/
    @BindView(R.id.email)
    AutoCompleteTextView mEmailView;
    @BindView(R.id.password)
    EditText mPasswordView;

    @BindView(R.id.swipeback)
    SwipeBackLayout swipeBackLayout;

    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void initView() {
        super.initView();
        setSupportActionBar(mToolbar);
        swipeBackLayout.setOnSwipeBackListener(new SwipeBackLayout.OnSwipeBackListener() {

            @Override
            public boolean onIntercept(int i, float v, float v1) {
                return false;
            }

            @Override
            public void onViewPositionChanged(float v) {

            }

            @Override
            public void onAnimationEnd() {
                finish();
                overridePendingTransition(0, android.R.anim.fade_out);
            }
        });

        mPasswordView.setOnEditorActionListener((textView, id, keyEvent) -> {
            if (id == R.id.login || id == EditorInfo.IME_NULL) {
                attemptLogin();
                return true;
            }
            return false;
        });

        findViewById(R.id.email_sign_in_button).setOnClickListener(view -> {
            view.requestFocus();
            attemptLogin();
        });

    }


    /**
     * Attempts to sign in or register the account specified by the login form.
     * If there are form errors (invalid email, missing fields, etc.), the
     * errors are presented and no actual login attempt is made.
     */
    private void attemptLogin() {

        // Reset errors.
        mEmailView.setError(null);
        mPasswordView.setError(null);

        // Store values at the time of the login attempt.
        String email = mEmailView.getText().toString();
        String password = mPasswordView.getText().toString();

        boolean cancel = false;

        // Check for a valid email address.
        if (TextUtils.isEmpty(email)) {
            mEmailView.setError(getString(R.string.error_field_required));
            mEmailView.requestFocus();
            cancel = true;
        }
        else if (!mPresenter.isEmailValid(email)) {
            mEmailView.setError(getString(R.string.error_invalid_email));
            mEmailView.requestFocus();
            cancel = true;
        }

        // Check for a valid password, if the user entered one.
        if (!TextUtils.isEmpty(password) && !mPresenter.isPasswordValid(password)) {
            mPasswordView.setError(getString(R.string.error_invalid_password));
            mPasswordView.requestFocus();
            cancel = true;
        }
        if (!cancel){
            mPresenter.login("1231231", "sdfaf12e");
        }

    }

    @Override
    public void loginSuccess() {

    }

    @Override
    public void signSuccess() {

    }

    @Override
    public void showMsg(String s) {
        Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showProgress(boolean isShow, String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }


}

